1.model目录新建Captcha.go
package models

import (
	"github.com/mojocn/base64Captcha"
	"image/color"
)

// Captcha 图形验证码
type Captcha struct {
	Id   string
	B64s string
}

var store = base64Captcha.DefaultMemStore

// GetCaptcha 获取验证码
func GetCaptcha() (id string, b64s string, err error) {
	bgcolor := color.RGBA{0, 0, 0, 0}
	fonts := []string{"wqy-microhei.ttc"}
	driver := base64Captcha.NewDriverMath(40, 102, 0, 0, &bgcolor, fonts)
	captcha := base64Captcha.NewCaptcha(driver, store)
	id, b64s, err = captcha.Generate()
	return id, b64s, err
}

// VerifyCaptcha 校验图形验证码
func VerifyCaptcha(id string, b64s string) bool {
	return store.Verify(id, b64s, true)
}

2.控制器输出json格式数据
func (c *Index) Get() {
	id, b64s, err := models.GetCaptcha()
	if err != nil {
		c.Data["json"] = err.Error()
	} else {
		c.Data["json"] = models.Captcha{id, b64s}
	}
	c.ServeJSON()
}

3.控制器验证码验证码
func (c *Index) Captcha() {
	if models.VerifyCaptcha(c.GetString("id"), c.GetString("b64s")) {
		c.Data["json"] = true
	} else {
		c.Data["json"] = false
	}
	c.ServeJSON()
}